/**
 * @program: yygh_parent
 * @ClassName DepartmentServiceImpl
 * @description:
 * @author: zyw
 * @create: 2023-05-25 09:40
 * @Version 1.0
 **/
package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import com.netflix.ribbon.proxy.annotation.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> map) {
        String s = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(s, Department.class);
        Department existDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (existDepartment == null) {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
        } else {
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            department.setCreateTime(existDepartment.getCreateTime());
            department.setId(existDepartment.getId());
        }
        departmentRepository.save(department);
    }

    @Override
    public Page<Department> getDepartmentPage(Map<String, Object> map) {
        String hostcode = (String)map.get("hostcode");
        Integer pageNum = Integer.parseInt((String) map.get("page"));
        Integer pageSize = Integer.parseInt((String) map.get("limit"));
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageRequest = PageRequest.of(pageNum-1, pageSize, sort);
        Department department = new Department();
        department.setHoscode(hostcode);
        Example<Department> example = Example.of(department);
        Page<Department> page = departmentRepository.findAll(example, pageRequest);
        return page;
    }

    @Override
    public void remove(Map<String, Object> map) {
        String hostcode = (String)map.get("hostcode");
        String depcode = (String) map.get("depcode");
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hostcode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> result=new ArrayList<>();
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example);
        //根据科室编号进行分组
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历集合
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //对应的全局数据
            List<Department> entryValue = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(entryValue.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> childDepartment = new ArrayList<>();
            for (Department department1 : entryValue) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department1.getDepcode());
                departmentVo2.setDepname(department1.getDepname());
                //封装到list
                childDepartment.add(departmentVo2);
            }
            //再把小科室封装到大科室列表
            departmentVo1.setChildren(childDepartment);
            result.add(departmentVo1);
        }
        return result;
    }

    @Override
    public String getDepname(String hoscode, String depcode) {
        Department department = departmentRepository.getDepnameByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }

}
 
